home *** CD-ROM | disk | FTP | other *** search
/ PC-SIG Library 8 / PC-SIG Library CD-ROM (8th Edition) (1990-04).iso / 201_300 / disk0258 / talk.asm < prev    next >
Encoding:
Assembly Source File  |  1984-05-02  |  11.0 KB  |  272 lines

  1.          name      talk
  2.          page      55,132
  3.          .lfcond             ;list false conditionals too
  4.          title     'TALK --- dumb terminal emulator'
  5. ;
  6. ; TALK --- a dump terminal emulator for the
  7. ; IBM PC, to illustrate use of the ROM BIOS
  8. ; asynchronous communications card support.
  9. ;
  10. ; Copyright (c) 1983 Ray Duncan
  11. ; May be freely reproduced for non-commercial use.
  12. ;
  13. cr       equ       0dh       ;ASCII carriage return
  14. lf       equ       0ah       ;ASCII line feed
  15. esc      equ       1bh       ;ASCII escape code
  16. ;
  17. echo     equ       0         ;leave this zero to run
  18.                              ;communications full-duplex,
  19.                              ;change to -1 if half-duplex
  20. ;
  21. comm_port  equ     0         ;set to 0 or 1 depending
  22.                              ;on which comm port is
  23.                              ;hooked to your modem
  24. ;
  25. ;
  26. cseg     segment   para public 'CODE'
  27. ;
  28.          assume    cs:cseg,ds:dseg,ss:stack
  29. ;
  30. talk     proc      far       ;entry point from PC-DOS
  31.                              ;
  32.          push      ds        ;save DS:0000 on stack
  33.          xor       ax,ax     ;for final exit to PC-DOS.
  34.          push      ax
  35.                              ;make data area addressable
  36.          mov       ax,seg dseg
  37.          mov       ds,ax
  38.  
  39.                              ;make sure the modem is on-line
  40.          call      com_stat  ;by checking the "Data Set
  41.                              ; Ready" bit in the status word.
  42.          test      al,20h
  43.          jnz       talk1     ;if bit is on, ok to proceed.
  44.                              ;
  45.                              ;bit is not on, print warning
  46.                              ;message and exit.
  47.          mov       dx,offset msg1
  48.          jmp       talk6
  49.                              ;
  50. talk1:                       ;initialize the display to
  51.                              ;reverse video, so the user
  52.                              ;knows he's talking to the modem.
  53.                              ;
  54.          mov       ah,15     ;first use "get mode" function
  55.                              ;of the ROM BIOS video driver to
  56.          int       10h       ;find the number of columns on
  57.          dec       ah        ;the display, save it for use
  58.                              ;by the screen clear routine.
  59.          mov       columns,ah
  60.          cmp       al,7      ;make sure display is text mode.
  61.          je        talk2     ;mode 7 ok, proceed
  62.          cmp       al,3
  63.          jbe       talk2     ;modes 0-3 ok,proceed
  64.          mov       dx,offset msg2
  65.          jmp       talk6     ;print error message and exit
  66.                              ;
  67. talk2:
  68.          mov       bh,70h    ;now clear screen and set to
  69.          call      clear     ;reverse video attribute 70h.
  70.                              ;also put the cursor in the
  71.          call      home      ;upper left corner of screen.
  72.                              ;
  73.                              ;****************************
  74.                              ;this is the main loop of
  75.                              ;the communications program.
  76.                              ;****************************
  77. talk3:   call      pc_stat   ;check character waiting
  78.                              ;from the IBM PC keyboard
  79.          jz        talk4     ;no
  80.          call      pc_in     ;read char. from PC keyboard
  81.          cmp       al,esc    ;is it the ESCape key?
  82.          je        talk5     ;yes, exit the terminal emulator
  83.  
  84.                              ;if running half-duplex, echo
  85.                              ;the character to the PC display
  86.          if        echo
  87.          push      ax        ;save copy of the character
  88.          call      pc_out    ;send it to the PC display
  89.          pop       ax        ;now restore the character
  90.          endif
  91.  
  92.          call      com_out   ;write char. to the comm port
  93.                              ;
  94. talk4:   call      com_stat  ;check if character waiting
  95.                              ;from the comm port
  96.          jz        talk3     ;no, loop
  97.          call      com_in    ;read char. from comm port
  98.          call      pc_out    ;write it to the PC display
  99.                              ;
  100.                              ;now do it all again
  101.          jmp       talk3
  102.                              ;
  103. talk5:                       ;ESC key detected, prepare
  104.                              ;to exit the terminal emulator.
  105.                              ;
  106.                              ;first set the display back
  107.                              ;to normal video, so the user
  108.                              ;will know he's talking to
  109.                              ;the PC and not the modem.
  110.  
  111.          mov       bh,07h    ;7 is the "attribute" for
  112.          call      clear     ;normal video display.
  113.          call      home      ;also put cursor in upper
  114.                              ;left hand corner of screen.
  115.                              ;
  116.                              ;print farewell message
  117.          mov       dx,offset msg3
  118. talk6:   mov       ah,9      ;use PC-DOS function 9 to
  119.          int       21h       ;print the string whose address
  120.                              ;is in register DX.
  121.          ret                 ;now return to PC-DOS.
  122.                              ;
  123. talk     endp
  124. ;
  125.                              ;this routine reads status
  126.                              ;from the COM port, returns
  127.                              ;Z=false if character ready
  128.                              ;Z=true if nothing waiting
  129.                              ;AH=line status,AL=modem status
  130.                              ;register DX destroyed.
  131. com_stat proc      near
  132.          mov       dx,comm_port
  133.          mov       ah,3      ;use ROM BIOS driver's function
  134.          int       14h       ;3 to get status, also
  135.          test      ah,098h   ;check comm port error flags
  136.                              ;for timeout,break,framing error.
  137.          jnz       com_err   ;error was detected, beep.
  138.  
  139. com_stat1:                   ;test the data ready bit,
  140.          test      ah,1      ;returning Z flag=false if
  141.          ret                 ;character waiting.
  142.  
  143. com_err: push      ax        ;communications error detected
  144.          mov       al,7
  145.          call      pc_out    ;send a bell code to the PC
  146.          pop       ax        ;then go return comm status
  147.          jmp       com_stat1
  148.  
  149. com_stat endp
  150. ;
  151.                              ;read a character from the
  152.                              ;COM port, return it in AL.
  153.                              ;register DX destroyed.
  154. com_in   proc      near
  155.          mov       dx,comm_port
  156.          mov       ah,2      ;use ROM BIOS driver's
  157.          int       14h       ;function 2 to get char.
  158.          ret
  159. com_in   endp
  160. ;
  161.                              ;write the character in AL
  162.                              ;to the COM port.
  163.                              ;register DX destroyed.
  164. com_out  proc      near
  165.          mov       dx,comm_port
  166.          mov       ah,1      ;use ROM BIOS driver's
  167.          int       14h       ;function 1 to send char.
  168.          ret
  169. com_out  endp
  170. ;
  171.                              ;read status for the IBM
  172.                              ;PC's keyboard, returns
  173.                              ;Z=false if character ready
  174.                              ;Z=true if nothing waiting.
  175.                              ;register DX destroyed.
  176. pc_stat  proc      near
  177.                              ;if a character is already
  178.                              ;waiting,just return status
  179.          mov       al,in_char
  180.          or        al,al
  181.          jnz       pc_stat1
  182.          mov       ah,6      ;otherwise call PC-DOS to
  183.          mov       dl,0ffh   ;determine status
  184.          int       21h
  185.          jz        pc_stat1  ;jump,nothing ready
  186.                              ;got a char, save it for
  187.                              ;"pc_in" routine.
  188.          mov       in_char,al
  189. pc_stat1:                    ;return to caller with
  190.          ret                 ;Z flag set appropriately
  191. pc_stat  endp
  192. ;
  193.                              ;read a character from the
  194.                              ;IBM PC's keyboard, return
  195.                              ;it in AL.  DX may be destroyed.
  196. pc_in    proc      near
  197.          mov       al,in_char
  198.          or        al,al     ;any character waiting?
  199.          jnz       pc_in1    ;yes,return it to caller
  200.          call      pc_stat   ;try and read a character
  201.          jmp       pc_in
  202. pc_in1:  xor       ah,ah     ;clear the character
  203.                              ;waiting flag
  204.          mov       in_char,ah
  205.          ret                 ;exit with AL=char
  206.  
  207. pc_in    endp
  208. ;
  209.                              ;write the character in AL
  210.                              ;to the PC's display.
  211.                              ;register DX destroyed.
  212. pc_out   proc      near
  213.          mov       dl,al     ;we use PC-DOS function 6 to
  214.          mov       ah,6      ;send the character, it ignores
  215.          int       21h       ;control characters so they
  216.          ret                 ;can be passed on to the
  217. pc_out   endp                ;remote system.
  218. ;
  219. clear    proc      near      ;clear the display and set
  220.                              ;it to the attribute in BH.
  221.                              ;registers AX, CX, DX destroyed.
  222.          mov       dl,columns
  223.          mov       dh,24     ;DL,DH=X,Y of lower right
  224.                              ;corner of "window".
  225.          mov       cx,0      ;CL,CH=X,Y of upper left
  226.                              ;corner of "window".
  227.          mov       ax,600h   ;AH=6 for "scroll or initialize
  228.                              ;window" function, AL=0 for
  229.                              ;number of lines to scroll.
  230.          int       10h       ;call ROM BIOS video driver.
  231.          ret
  232. clear    endp
  233. ;
  234.                              ;home the cursor, i.e.
  235.                              ;put it in the upper left
  236.                              ;corner of the screen.
  237.                              ;registers AX,BX,DX destroyed.
  238. home     proc      near
  239.          mov       dx,0      ;(DL,DH)=(X,Y) for new
  240.                              ;cursor position, both are 0.
  241.          mov       bh,0      ;BH=video page
  242.          mov       ah,2      ;function 2=set cursor
  243.          int       10h       ;call ROM BIOS video driver.
  244.          ret
  245. home     endp
  246. ;
  247. cseg     ends
  248. ;
  249. ;
  250. dseg     segment   para 'DATA'
  251.                              ;
  252. in_char  db        0         ;PC keyboard input char.
  253.                              ;
  254. columns  db        0         ;highest numbered column in
  255.                              ;current display mode (39 or 79)
  256.                              ;
  257. msg1     db        cr,lf,'Check your modem.'
  258.          db        cr,lf,'$'
  259. msg2     db        cr,lf,'Display must be text mode.'
  260.          db        cr,lf,'$'
  261. msg3     db        cr,lf,lf,'Goodbye.'
  262.          db        cr,lf,'$'
  263. dseg     ends
  264. ;
  265. ;
  266. stack    segment   para stack 'STACK'
  267.                              ;allow 64 bytes in this case
  268.          db        64 dup (?)
  269. stack    ends
  270. ;
  271.          end       talk
  272.